package com.love.marriage.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 微信服务配置（公众号和小程序的基础服务）
 */
@Configuration
public class WxServiceConfig {

    @Autowired
    private WeChatConfig weChatConfig;

    @Autowired
    private WeChatGzhConfig weChatGzhConfig;

    /**
     * 公众号服务
     */
    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    /**
     * 公众号配置
     */
    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpDefaultConfigImpl wxMpConfigStorage = new WxMpDefaultConfigImpl();
        wxMpConfigStorage.setAppId(weChatGzhConfig.getAppId());
        wxMpConfigStorage.setSecret(weChatGzhConfig.getSecret());
        wxMpConfigStorage.useStableAccessToken(true);
        return wxMpConfigStorage;
    }

    /**
     * 小程序服务
     */
    @Bean
    public WxMaService wxMaService() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(weChatConfig.getAppId());
        config.setSecret(weChatConfig.getSecret());
        config.useStableAccessToken(true);
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        return service;
    }
} 